<template>
  <view class="container">
    <TopBtn :title="'整理病历'"></TopBtn>
    <patientCard class="" :data="patientdata"></patientCard>
    <view class="info mt_30">
      <view class="currency_card">
        <view class="CARDtitle flex-sp-center" @click="goinfo('pages/CommonTools/ChooseADisease/ChooseADisease')">
          <view class="currency_title">
            <text>*</text>
            疾病名称
            <!-- {{diseaseId}} -->
          </view>
          <!-- <RadioPicker v-model="baseFormData.diseaseId" :inputName="baseFormData.diseaseId" :Radio="'matchIncome'"
						:range="queryAllDiseaseList"></RadioPicker> -->

          <view class="uni-input flex-center">
            <text class="InputText" v-if="baseFormData.diseaseId">{{ names }}</text>
            <text v-else class="placeholder">请选择</text>
            <uni-icons type="right" color="#999" size="16"></uni-icons>
          </view>
        </view>

        <view class="currency_title flex-sp-center">
          <view class="">
            <text>*</text>
            患者主诉
          </view>
        </view>
        <view class="mt_30">
          <textArea v-model="baseFormData.chiefComplaint" :inputNane="baseFormData.chiefComplaint" class="mb_30"
            :maxNum="200"></textArea>
        </view>
      </view>
      <view class="currency_card">
        <view class="currency_title flex-sp-center">
          <view class="">
            <text>*</text>
            病历概要
          </view>
        </view>
        <view class="mt_30">
          <textArea v-model="baseFormData.summary" :inputNane="baseFormData.summary" class="mb_30"
            :maxNum="500"></textArea>
        </view>
      </view>
      <view class="currency_card">
        <view class="currency_title flex-sp-center">
          <view class="">
            <text>*</text>
            初步诊断
          </view>
        </view>
        <view class="mt_30">
          <textArea v-model="baseFormData.primaryDiagnosis" :inputNane="baseFormData.primaryDiagnosis" class="mb_30"
            :maxNum="500"></textArea>
        </view>
      </view>
      <view class="currency_card">
        <view class="currency_title flex-sp-center">
          <view class="">
            <text>*</text>
            诊断意见
          </view>
        </view>
        <view class="mt_30">
          <textArea v-model="baseFormData.diagnosticOpinion" :inputNane="baseFormData.diagnosticOpinion" class="mb_30"
            :maxNum="500"></textArea>
        </view>
      </view>
      <view class="currency_card">
        <view class="currency_title flex-sp-center">
          <view class="">现病史</view>
        </view>
        <view class="mt_30">
          <textArea v-model="baseFormData.presentIllness" :inputNane="baseFormData.presentIllness" class="mb_30"
            :maxNum="500"></textArea>
        </view>
      </view>
      <view class="currency_card">
        <view class="currency_title flex-sp-center">
          <view class="">既往史</view>
        </view>
        <view class="mt_30">
          <textArea v-model="baseFormData.pastHistory" :inputNane="baseFormData.pastHistory" class="mb_30"
            :maxNum="500"></textArea>
        </view>
      </view>
      <view class="btn" @click="createMedicalRecord()">保存</view>
    </view>
  </view>
</template>

<script>
import {
  SendGroupMessage
} from '@/utils/TIM'
import {
  createMedicalRecord,
  getPatientInfoByOutId,
  queryAllDisease,
  getMedicalRecord,
  updateMedicalRecord,
} from '@/api/ConsultationRoom'
import {
  getDiseaseControlList
} from '@/api/user'
export default {
  data () {
    return {
      baseFormData: {
        diseaseId: '',
      },
      patientdata: {},
      queryAllDiseaseList: [],
      ids: '',
      names: '',
    }
  },
  onShow () {
    let that = this
    uni.$once('ids', function (data) {
      this.$set(that.baseFormData, 'diseaseId', data)
      that.getDiseaseControlList()
    })
  },
  onUnload () {
    uni.$off()
  },
  onLoad (e) {
    if (e.id) {
      this.baseFormData.outpatientId = e.id
    }
    if (e.groupId) {
      this.groupId = e.groupId
    }
    if (e.patientId) {
      this.baseFormData.patientId = e.patientId
    }
    if (e.prescriptionId) {
      this.baseFormData.prescriptionId = e.prescriptionId
    }
    this.getPatientInfoByOutId()
    this.queryAllDisease()
    this.getMedicalRecord()
  },
  methods: {
    getDiseaseControlList () {
      getDiseaseControlList({
        ids: this.baseFormData.diseaseId,
      }).then(res => {
        console.log(res)
        var name = []
        res.data.forEach(item => {
          name.push(item.name)
        })
        this.names = name.join(',')
      })
    },
    queryAllDisease () {
      queryAllDisease().then(res => {
        this.queryAllDiseaseList = res.data
      })
    },

    getMedicalRecord () {
      getMedicalRecord({
        outpatientId: this.baseFormData.outpatientId,
      }).then(res => {
        if (res.data) {
          this.baseFormData = res.data
          this.names = res.data.diseaseName
        }
      })
    },
    getPatientInfoByOutId () {
      var id = {}
      id.outpatientId = this.baseFormData.outpatientId
      getPatientInfoByOutId(id).then(res => {
        this.patientdata = res.data
        this.baseFormData.summary = `${res.data.patientName} , ${res.data.patientName == 1 ? '男' : '女'
          } , ${res.data.patientAge}岁 ${res.data.consultationFormRespVO
            ? ', ' + res.data.consultationFormRespVO.diseaseDescribe
            : ''
          }`
      })
    },
    createMedicalRecord () {
      if (!this.baseFormData.chiefComplaint) {
        uni.showToast({
          title: '请填写主诉',
          icon: 'none',
          duration: 2000,
          mask: true,
        })
        return
      }
      if (!this.baseFormData.summary) {
        uni.showToast({
          title: '病历概要',
          icon: 'none',
          duration: 2000,
          mask: true,
        })
        return
      }
      if (!this.baseFormData.primaryDiagnosis) {
        uni.showToast({
          title: '初步诊断',
          icon: 'none',
          duration: 2000,
          mask: true,
        })
        return
      }
      if (this.baseFormData.id) {
        updateMedicalRecord(this.baseFormData).then(res => {
          uni.showToast({
            title: '修改成功',
            icon: 'none',
            duration: 2000,
            mask: true,
          })

          // 发送群消息
          this.sendMessage()

          setTimeout(() => {
            //需要定时执行的代码
            uni.$emit('show', true)
            uni.navigateBack({
              delta: 1, //返回层数，2则上上页
            })
          }, 2000)
        })
      } else {
        createMedicalRecord(this.baseFormData).then(res => {
          uni.showToast({
            title: '提交成功',
            icon: 'none',
            duration: 2000,
            mask: true,
          })

          // 发送群消息
          this.sendMessage()

          setTimeout(() => {
            //需要定时执行的代码
            uni.$emit('show', true)
            uni.navigateBack({
              delta: 1, //返回层数，2则上上页
            })
          }, 2000)
        })
      }
    },
    goinfo (e, id) {
      if (id) {
        uni.navigateTo({
          url: `/${e}?id=${id}`,
        })
      } else {
        uni.navigateTo({
          url: `/${e}`,
        })
      }
    },
    sendMessage () {
      // 发送群消息
      const message = {
        businessID: 'tip',
        type: 'card',
        title: '请确认病历',
        text: '请确认病历是否与您描述的症状一致',
        buttonText: '去查看',
        buttonUrl: {
          patient: `/pagesCommon/case/caseDetails?id=${this.baseFormData.outpatientId}`,
          doctor: `/pages/ConsultationRoom/medicalRecord/medicalRecord?id=${this.baseFormData.outpatientId}`,
        },
      }
      SendGroupMessage(this.groupId, message)
    },
  },
}
</script>

<style lang="less" scoped>
.uni-input {
  display: flex;
  justify-content: right;
  align-items: center;
  height: 100%;
  height: 70rpx;
  font-size: 30rpx;
  font-family: PingFang SC;
  font-weight: 500;

  .placeholder {
    color: #999;
  }

  .InputText {
    width: 400rpx;
    font-size: 26rpx;
  }
}

.CARDtitle {
  font-weight: 500;
  font-size: 28rpx;
  color: #666666;
  padding: 10rpx 0 30rpx;
}

.currency_title {
  text {
    color: #ff0303;
    margin-right: 10rpx;
  }
}

.btn {
  width: 649rpx;
  height: 100rpx;
  background: #3b6cfe;
  border-radius: 50rpx;
  font-weight: 500;
  font-size: 28rpx;
  color: #ffffff;
  text-align: center;
  line-height: 100rpx;
  margin: 20rpx auto 0;
}

.container {
  padding-top: 158rpx;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  box-sizing: border-box;
  min-height: 100vh;

  .info {
    flex: 1;
    width: 750rpx;
    background: #ffffff;
    border-radius: 52rpx 52rpx 0 0;
    padding: 30rpx;
    box-sizing: border-box;

    .text {
      font-weight: 500;
      font-size: 24rpx;
      color: #666666;
    }

    /deep/.is-input-border {
      border: none;
    }

    /deep/.uni-easyinput__content-input {
      text-align: right;
      padding-right: 10rpx;
    }

    /deep/ .uni-forms-item__label {
      font-weight: bold;
      font-size: 30rpx;
      color: #333333;
    }

    /deep/ .uni-forms-item {
      margin-bottom: 0rpx;
    }

    /deep/.uni-forms-item__content {
      display: flex;
      align-items: center;
      justify-content: right;
    }

    /deep/.checklist-group {
      justify-content: right;
    }
  }
}
</style>